package pod

import (
	"context"
	"fmt"

	"go-pod/api/internal/svc"
	"go-pod/api/internal/types"
	"go-pod/pod-rpc/pod"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePodcastLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdatePodcastLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePodcastLogic {
	return &UpdatePodcastLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdatePodcastLogic) UpdatePodcast(req *types.PodUpdateReq) (resp *types.PodUpdateResp, err error) {
	// 参数校验
	if req.Id == 0 {
		return nil, fmt.Errorf("播客ID不能为空")
	}
	if req.Content == "" {
		return nil, fmt.Errorf("内容不能为空")
	}

	// 调用RPC服务更新播客
	_, err = l.svcCtx.PodRpcClient.UpdatePodcast(l.ctx, &pod.UpdatePodcastReq{
		Id:      req.Id,
		Content: req.Content,
	})
	if err != nil {
		l.Logger.Errorf("更新播客失败: %v", err)
		return nil, fmt.Errorf("更新播客失败，请稍后重试")
	}

	return &types.PodUpdateResp{}, nil
}
